// pages/register/register.js
var app=getApp();
var ea  = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: '',
    width: 250,//宽度
    height: 250,//高度
    status: false,

    getguanxi: app.data.url +'/zhongxue/maoapi/getguanxi',
    guanxi:[],
    index: 0,
    kong:true,
    datas:{
     user:'',
    phones:'',
    pwd:'',          
    address:'',      //家庭住址
    child_name:'',   //孩子姓名
    relationship:'', //关系
    sex:'',         //性别
      birthday:'2016-09-01',    //生日
    touxiang:'',  //头像
    study:'',  //学习状况
    bodys:'',   //身体状况
    },

    date: '2016-09-01',
    gister: app.data.url + '/zhongxue/maoapi/register',
    upload: app.data.url + '/zhongxue/maoapi/getqiniutoken',
    jiajia:app.data.url +"/uploads/zhongxue/jiajia.png",
    items:[
    {name:"男",chceked:true},
     { name: "女"}
    ],
    array: ["母子","母女","父女","父子","其他"],
    arrays: ["6岁", "7岁", "8岁", "9岁", "10岁", "11岁", "12岁", "13岁", "14岁", "15岁", "16岁", "17岁", "18岁", "19岁", "20岁","其他"],
  },

  //获取参数 
  bindinputs:function(e){
    var value=e.detail.value;
    var key = e.currentTarget.dataset.key;
    var datas = this.data.datas;
    datas[key] = value;
    this.setData({datas:datas});  
  },
   // 获取性别
  radioChanges:function(e){
   var es =e.detail.value;
   var dates=this.data.datas;
   dates["sex"]=es;
   this.setData({
     datas: dates
   })
  },
  //获取生日
  bindDateChange:function(e){
   let that=this;
   var datas=that.data.datas;
    datas["birthday"] = e.detail.value;
    console.log(datas);
    that.setData({
      datas:datas,
      date: e.detail.value
    })
  },
  
  //获取亲属关系
  bindPickerChanges:function(e){
    var value = e.detail.value;
    var guanxi = this.data.guanxi;
    var values = guanxi[value].id
    var datas = this.data.datas;
    datas["relationship"] = values;
    console.log(datas);
    this.setData({ datas: datas ,index:value});  
  },


  //获取头像的照片
  uploade: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      // sizeType: [],
      // sourceType: [],
      success: function (res) {
        var src = res.tempFilePaths[0];
        console.log(112313213131)
        console.log(src);
        that.setData({ status: true })
        that.cropper = that.selectComponent("#image-cropper");
        that.setData({
          src: src,
        });
        wx.showLoading({
          title: '加载中'
        })
      },
      fail: function (res) { 
        console.log('选择失败')
        console.log(res);
      },
      complete: function (res) { },
    })
    console.log('执行过了')
    // wx.chooseImage({
    //   count: 1, // 默认9
    //   sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
    //   sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
    //   success: res => {
    //     wx.showLoading({
    //       title: '上传中',
    //     })
    //     // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
    //     let tempFilePaths = res.tempFilePaths;
    //     let datas = that.data.datas;
    //     that.setData({
    //       jiajia: tempFilePaths[0]
    //     })
    //     console.log(tempFilePaths);
        // ea.upfile(tempFilePaths,function(filename){
        //   var names = filename[0];
        //   var datas = that.data.datas;
        //   datas.touxiang = names;
        //   wx.hideLoading();
        // },that.data.upload)

    //   }
    // })
  },
   



   //学习状况
  qis:function(e){
   var vale=e.detail.value;
   var datas=this.data.datas;
   datas["study"]=vale;
   this.setData({
     datas:datas
   })  

  },
  //身体状况
  qiss: function (e) {
    var vale = e.detail.value;
    var datas = this.data.datas;
    datas["bodys"] = vale;
    this.setData({
      datas: datas
    })
  },

  

//注册
  register:function(e){
    var that =this;
    var kong = this.data.kong;
    if(!kong){
      return
    }
    var tss=that.data.datas;
    this.setData({kong:false});
    // console.log(tss);
    // return
    tss.token=wx.getStorageSync("user").token;
    wx.request({
      url: this.data.gister,
      data: tss,
      method:'post',
      success:function(res){
       if(res.data.code==200){
         wx.setStorageSync('user', res.data.data);
         wx.showToast({
           title: '注册成功',
           icon: 'none',
           duration: app.data.quanbushijian
         })
        setTimeout(function(){
          wx.navigateTo({
            url: '/pages/login/login',
          })
        },3000)
       }else{
         that.setData({kong:true})
         var ress = res.data.msg;
         wx.showToast({
           title: ''+ress+'',
           icon:'none',
           duration: app.data.quanbushijian
         })
       }
      }
    })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync('user');
    var that  = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getguanxi,
      data:{token:user.token},
      method:"POST",
      success:function(res){
        var data = res.data;
        if(data.code==200){
          var shuju = data.data;
          var datas = that.data.datas;
          datas.relationship = shuju[0].id;
          that.setData({ guanxi: shuju})
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none',
            duration: app.data.quanbushijian
          })
        }
      },
      complete:function(){
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/indexfirst/indexfirst',
    }
  },
  cropperload(e) {
    console.log("cropper初始化完成");
  },
  loadimage(e) {
    console.log("图片加载完成", e.detail);
    wx.hideLoading();
    //重置图片角度、缩放、位置
    this.cropper.imgReset();
  },
  
  clickcut(e) {
    console.log(e.detail);
    //点击裁剪框阅览图片
    console.log(e.detail.url);
    var that = this;
    wx.showLoading({
      title: '上传中',
    })
    ea.upfile([e.detail.url], function (filename) {
      var names = filename[0];
      var datas = that.data.datas;
      datas.touxiang = names;
      console.log(datas);
      that.setData({ datas: datas, status:false});
      wx.hideLoading();
    }, that.data.upload)
    // this.setData({ xinsrc: e.detail.url, status: false })

    // wx.previewImage({
    //   current: e.detail.url, // 当前显示图片的http链接
    //   urls: [e.detail.url] // 需要预览的图片http链接列表
    // })
  },
})